/*
 * iShopPlace Project.
 */
package ishopplace.model.dao.hibernate;

import ishopplace.model.businessobject.Category;
import ishopplace.model.businessobject.Item;
import ishopplace.model.businessobject.Product;
import ishopplace.model.dao.CatalogDao;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * The Hibernate implementation of the <code>CatalogDao</code>.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 * @see CatalogDao
 */
public class CatalogDaoHibernateImpl extends HibernateDaoSupport implements CatalogDao {
	public List getCategoryList() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				StringBuffer sb = new StringBuffer(100);
				sb.append("select distinct category ");
				sb.append("from Category category ");
				sb.append("order by category.categoryId");

				return session.createQuery(sb.toString()).list();
			}
		});		
	}
	
	public Category getCategory(String categoryId) {
		return (Category)this.getHibernateTemplate().load(Category.class, categoryId);
	}
	
	public List getProductListByCategory(final String categoryId) {
		return (List)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				StringBuffer sb = new StringBuffer(100);
				sb.append("select distinct product ");
				sb.append("from Product product ");
				sb.append("where product.categoryId = :id ");
				sb.append("order by product.productId");
				
				return session.createQuery(sb.toString()).setParameter("id", categoryId).list();
			}
		});
	}
	
	public Product getProduct(String productId) {
		return (Product)this.getHibernateTemplate().load(Product.class, productId);
	}
	
	public List getItemListByProduct(final String productId) {
		return (List)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				StringBuffer sb = new StringBuffer(100);
				sb.append("select distinct item ");
				sb.append("from Item item ");
				sb.append("where item.product.productId = :id ");
				sb.append("order by item.itemId");
				
				return session.createQuery(sb.toString()).setParameter("id", productId).list();
			}
		});
	}
	
	public Item getItem(String itemId) {
		return (Item)this.getHibernateTemplate().load(Item.class, itemId);
	}
	
	public List searchProductList(final List keywords) {
		if (keywords == null || keywords.isEmpty()) {
			return new ArrayList();
		}
		
		return (List)getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				StringBuffer sb = new StringBuffer(100);
				sb.append("select distinct product ");
				sb.append("from Product product ");
				sb.append("where ");
				
				for (int i=0; i<keywords.size(); i++) {
					String keyword = (String)keywords.get(i);
					sb.append("product.name like ");
					sb.append("'%").append(keyword).append("%' ");
					sb.append("OR ");
				}
				
				for (int i=0; i<keywords.size(); i++) {
					String keyword = (String)keywords.get(i);
					sb.append("product.categoryId like ");
					sb.append("'%").append(keyword).append("%' ");
					sb.append("OR ");
				}
				
				for (int i=0; i<keywords.size(); i++) {
					String keyword = (String)keywords.get(i);
					sb.append("product.descriptionWithImage like ");
					sb.append("'%").append(keyword).append("%' ");
					
					if (i < keywords.size() -1) {
						sb.append("OR ");
					}
				}
				
				sb.append("order by product.productId");
				return session.createQuery(sb.toString());
			}
		});
	}
}
